package com.lpf.demo;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.python.core.Py;
import org.python.core.PyFunction;
import org.python.core.PyObject;
import org.python.util.PythonInterpreter;

import java.io.IOException;

import static com.lpf.util.CodeUtils.decodeUTF8Str;

public class DemoPython02 {

    public static void main(String[] args) throws IOException {

        String pythonFunc = "E:\\IdeaProjects\\java-learning\\07-java-python\\src\\main\\resources\\demo\\demoPython02.py";

        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.execfile(pythonFunc);
        PyFunction pyf = interpreter.get("add", PyFunction.class);

        PyObject[] pyObjects = new PyObject[1];
        pyObjects[0] = Py.newStringUTF8(JSONUtil.toJsonStr(MapUtil.of("key", "单点")));

        PyObject Res = pyf.__call__(pyObjects);
        JSONObject jsonObject = JSONUtil.parseObj(decodeUTF8Str(Res.toString()));
        System.out.println(jsonObject);
        interpreter.cleanup();
    }

}